package  com.qyer.log.job.sort;
import java.io.IOException;
import java.net.URISyntaxException;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

import com.qyer.util.log.common.Constants;
import com.qyer.util.log.util.HiveFormatUtil;
import com.qyer.util.log.util.MyDateUtil;

public class SeachShowPhrase {
	/**
	 * 列
	*/
	public static class M1 extends Mapper<LongWritable, Text, Text, Text> {
		Text oKey = new Text();
		Text oVal = new Text();
		public void map(LongWritable key, Text value, Context context) throws IOException,InterruptedException {
			try {
				String [] fields =  StringUtils.splitByWholeSeparatorPreserveAllTokens(value.toString(),"#|~" ) ;
				if (fields.length<6){
					return ; 
				}
				
				StringBuilder  outStr = new StringBuilder ("");
				outStr.append(HiveFormatUtil.getFeild(fields[0])).append(Constants.FS);
				outStr.append(HiveFormatUtil.getFeild(fields[1])).append(Constants.FS);
				outStr.append(HiveFormatUtil.getFeild(fields[2])).append(Constants.FS);
				outStr.append(HiveFormatUtil.getFeild(fields[3])).append(Constants.FS);
				outStr.append(HiveFormatUtil.getFeild(fields[4])).append(Constants.FS);
				outStr.append(HiveFormatUtil.getFeild(fields[5])).append(Constants.FS);
				
				String hour = fields[0].substring(8,10);
				
				outStr.append(HiveFormatUtil.getFeild(hour));
				
				oKey.set(outStr.toString());
				
				context.write(oKey, oVal);
			} catch (Exception e) {
			}
		}
	}

	
	public static class R1 extends Reducer<Text, Text, Text, Text> {
		Text oKey = new Text();
		Text oVal = new Text();
		String DATE;
		final String FS = "\t";
		
		public void reduce(Text key, Iterable<Text> values, Context context) throws IOException,
				InterruptedException {
			oKey.set(key);
			context.write(oKey, oVal);
		}
	}

	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException,
			URISyntaxException {

		Configuration conf = new Configuration();
//		conf.set("mapred.job.queue.name", "regular"); // default,regular,realtime
		conf.set("mapreduce.reduce.memory.mb" ,"2048");
		String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
		int exitCode = 127;
		
		if (otherArgs.length != 1) {
			System.out.println("************************************************************");
			System.out.println("************************************************************");
			System.out.println("Usage: please input 1 params, for example: file.jar args[0]");
			System.out.println("args[0] is dateList: 20130101,20130106 or 20130106");
			System.out.println("************************************************************");
			System.out.println("************************************************************");
			System.exit(exitCode);
		}
		
		String startDate = otherArgs[0].split(",")[0];
		String endDate = otherArgs[0].split(",").length == 2 ? otherArgs[0].split(",")[1] : startDate;

		String baseInPath01 = "/flume/original/search_phrase/";
		String baseOutPath = "/flume/clean/search_phrase/";
		 
		// 例行调度：昨天的日期
		for (String runDate :  MyDateUtil.getDateList(startDate, endDate, 0)) {
			conf.set("DATE", runDate);
			Job job = new Job(conf, "SeachShowPhrase");
			job.setJarByClass(SeachShowPhrase.class);
			job.setMapperClass(M1.class);
			job.setReducerClass(R1.class);
			job.setNumReduceTasks(1);
			job.setOutputKeyClass(Text.class);
			job.setOutputValueClass(Text.class);
			job.setMapOutputKeyClass(Text.class);
			job.setMapOutputValueClass(Text.class);
			
			FileInputFormat.addInputPath(job, new Path(baseInPath01 + "/" + runDate));

			String outPath = baseOutPath + "/" + runDate;
			FileSystem.get(conf).delete(new Path(outPath), true);
			FileOutputFormat.setOutputPath(job, new Path(outPath));
			
			 //启用压缩
	        FileOutputFormat.setCompressOutput(job, true);
	        //lzo格式
	        FileOutputFormat.setOutputCompressorClass(job, com.hadoop.compression.lzo.LzopCodec.class); 
			
			exitCode = job.waitForCompletion(true) ? 0 : 1;
			System.out.println("--------------------------------------------END1");

		}
		System.exit(exitCode);

	}

}
